<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/12/10
 * Time: 13:36
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Config;

class ConfigController extends Controller
{
    /**
     * 获取所有配置信息
     */
    public function getAll(){
        $list = Config::get();
        $newArray = array();
        foreach ($list as $key => $item){
            $newArray[$item['name']] = $item['value'];
        }
        return $this->returnJson(0,'',$newArray);
    }

    /**
     * 获取msg(公众号url)配置信息
     */
    public function getMsg(){
        $list = Config::where('type','msg')->get();
        return $this->returnJson(0,'',$list);
    }
    /**
     * 更新配置项信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(){
        $params = request()->all();
        if(isset($params['id']) && $params['id'] > 0){
            // 更新
            $model = Config::find($params['id']);
            if(!$model){
                return $this->returnJson(1,'error');
            }
            $result = $model->update($params);
            if($result){
                return $this->returnJson(0);
            }
            return $this->returnJson(1,'error');
        }else{
            // 新增
            if(!isset($params['name']) || $params['name'] == ''){
                return $this->returnJson(1,'error');
            }
            $isFind = Config::where('name',$params['name'])->first();
            if($isFind){
                return $this->returnJson(1,'NAME 已存在');
            }
            $result = Config::create($params);
            if($result){
                return $this->returnJson(0);
            }
        }
        return $this->returnJson(1,'error');
    }

}
